package com.yl.action;

import com.opensymphony.xwork2.ActionSupport;
import com.yl.core.UserService;
import com.yl.model.User;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;

import javax.security.auth.login.AccountLockedException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * User: Dream.YnagLong Date: 13-12-12 Time: 下午8:25
 */
@SuppressWarnings("serial")
public class UserAction extends ActionSupport {
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpServletResponse response = ServletActionContext.getResponse();
	private static Logger logger = Logger.getLogger(UserAction.class);
	private UserService userService;
	private User user;
	private String time;//预防DEV模式报错

	public String userLogin() {
		logger.info("登陆");
		//user.setAccount(request.getParameter("account"));
		//user.setPwd(request.getParameter("pwd"));
/*		String msg = "";
		user = userService.login(user);
		if (user == null)
			msg = "查无此人";
		else
			msg = "用户登陆成功！" + user.toString();
		request.setAttribute("msg", msg);*/
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
        String error = null;
        String viewName = "login";
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {//已认证且用户手动打开登录页
            viewName = "index";
        } else {
            if (UnknownAccountException.class.getSimpleName().equals(exceptionClassName)) {
                error = "用户名不存在！";
            } else if (IncorrectCredentialsException.class.getSimpleName().equals(exceptionClassName)) {
                error = "用户名/密码错误！";
            } else if (AccountLockedException.class.getSimpleName().equals(exceptionClassName)) {
                error = "帐号已经锁定";
            }else if (exceptionClassName != null) {
                error = "其他错误！请稍后重试！";
            }
        }
        request.setAttribute("error", error);
		return viewName;
	}

	public String userRegister() {
		logger.info("注册");
		//user.setAccount(request.getParameter("account"));
		//user.setUsername(request.getParameter("username"));
		//user.setPwd(request.getParameter("pwd"));
		String msg="注册成功！";
		userService.register(user);
		msg=msg+"用户ID为"+user.getId();
		request.setAttribute("msg", msg);
		//回显
		user=null;
		request.setAttribute("users", userService.getUsers(user));
		return "tomsg";
	}
	
	public String getUsers(){
		//获取用户列表
		request.setAttribute("users", userService.getUsers(user));
		return "tomsg";
	}
	public String updateUser(){
		//更新用户
		userService.updateUser(user);
		request.setAttribute("msg", "更新成功");
		return "tomsg";
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}
    
}
